package com.nondo.dean.dealer.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.dean.common.tool.AliyunOssTool;
import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.dealer.domain.StoreProduct;
import com.nondo.dean.dealer.domain.StoreProductExample;
import com.nondo.dean.dealer.mapper.StoreProductMapper;
import com.nondo.dean.dealer.model.OrderDetailBean;
import com.nondo.dean.dealer.model.StoreProductBean;
import com.nondo.dean.dealer.model.result.ActivityProductResult;
import com.nondo.dean.dealer.model.result.ProductResult;
import com.nondo.dean.dealer.model.result.StoreProductResult;

/**
 * 店铺产品业务管理类
 * @author zhaolijin
 * @date 2018年4月26日
 */
@Component
public class StoreProductService {
    
    @Autowired
    UserService userService;
    
    @Autowired
    AliyunOssTool aliyunOssTool;
    
    @Autowired
    ProductService productService;

    @Autowired
    StoreProductMapper storeProductMapper;

    /**
     * 添加
     *
     * @param storeProduct
     * @return
     */
    public int add(StoreProduct storeProduct) {
        return this.storeProductMapper.insertSelective(storeProduct);
    }

    /**
     * 更新
     *
     * @param storeProduct
     * @return
     */
    @Transactional
    public int update(StoreProduct storeProduct) {
        return this.storeProductMapper.updateByPrimaryKeySelective(storeProduct);
    }

    /**
     * 删除
     *
     * @param storeProduct
     * @return
     */
    public int delete(StoreProduct storeProduct) {
        storeProduct.setDelFlag(StoreProduct.DEL_FLAG_DELETE);
        return this.storeProductMapper.updateByPrimaryKeySelective(storeProduct);
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    public StoreProduct select(Integer id) {
        return this.storeProductMapper.selectByPrimaryKey(id);
    }

    /**
     * 获取列表
     *
     * @param offset
     * @param limit
     * @param storeProduct
     * @return
     */
    public List<StoreProduct> getList(int offset, int limit, StoreProduct storeProduct) {
        StoreProductExample example = new StoreProductExample();
        example.setOrderByClause("create_date DESC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        StoreProductExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, storeProduct);
        return storeProductMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 数据统计
     *
     * @param storeProduct
     * @return
     */
    public Long count(StoreProduct storeProduct) {
        StoreProductExample example = new StoreProductExample();
        StoreProductExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, storeProduct);
        return storeProductMapper.countByExample(example);
    }

    /**
     * 设置查询条件
     *
     * @param criteria
     * @param storeProduct
     */
    void setCriteria(StoreProductExample.Criteria criteria, StoreProduct storeProduct) {
        if (storeProduct != null) {
            if (storeProduct.getId() != null) {
                criteria.andIdEqualTo(storeProduct.getId());
            }
            if (storeProduct.getStoreId() != null) {
                criteria.andStoreIdEqualTo(storeProduct.getStoreId());
            }
            if (storeProduct.getProductId() != null) {
                criteria.andProductIdEqualTo(storeProduct.getProductId());
            }
            if (storeProduct.getShelfStatus() != null) {
                criteria.andShelfStatusEqualTo(storeProduct.getShelfStatus());
            }
            if (StringUtils.isNotBlank(storeProduct.getProductCode())) {
                criteria.andProductCodeLike("%" + storeProduct.getProductCode() + "%");
            }
            if (StringUtils.isNotBlank(storeProduct.getProductName())) {
                criteria.andProductNameLike("%" + storeProduct.getProductName() + "%");
            }
        }
        criteria.andDelFlagEqualTo(StoreProduct.DEL_FLAG_NORMAL);
    }

    /**
     * 将采购产品写入店铺产品
     * @param storeId 
     * @param orderDetailList List<{@link OrderDetailBean}>
     * @return 成功写入条数
     */
    @Transactional
    public int savePurchaseProduct(Integer storeId, List<OrderDetailBean> orderDetailList) {
        int num = 0;
        for (OrderDetailBean orderDetail : orderDetailList) {
            Integer productId = orderDetail.getProductId();
            boolean isExist = checkIsNotExist(storeId,productId);
            if (!isExist) {
                StoreProduct storeProduct = new StoreProduct();
                BeanUtils.copy(orderDetail, storeProduct);
                storeProduct.setShelfStatus(1);
                storeProduct.setStoreId(storeId);
                storeProduct.setSalePrice(orderDetail.getProductPrice());
                int update = storeProductMapper.insertSelective(storeProduct);
                num = (update > 0) ? num + 1 : num;
            }
        }
        return num;
    }

    /**
     * 检查店铺中该商品是否已存在
     * @param storeId
     * @param productId
     * @return true 存在
     */
    private boolean checkIsNotExist(Integer storeId, Integer productId) {
        StoreProductExample example = new StoreProductExample();
        StoreProductExample.Criteria criteria = example.createCriteria();
        StoreProduct storeProduct = new StoreProduct();
        storeProduct.setStoreId(storeId);
        storeProduct.setProductId(productId);
        setCriteria(criteria, storeProduct);
        long num = storeProductMapper.countByExample(example);
        return (num > 0) ? true : false;
    }
    
    /**
     * 获取店铺产品BEAN列表
     * @param storeProductList List<{@link StoreProduct}>
     * @return List<{@link StoreProductResult}>
     */
    public List<StoreProductResult> getStoreProductResultList(List<StoreProduct> storeProductList) {
        List<StoreProductResult> storeProductResultList = new ArrayList<StoreProductResult>();

        for (StoreProduct storeProductDb : storeProductList) {
            ProductResult productResult = productService.getProductBeanById(storeProductDb.getProductId());
            StoreProductResult storeProductResult = StoreProductResult.covertToBean(productResult);
            storeProductResult.setId(storeProductDb.getId());
            storeProductResult.setCostPrice(productResult.getSalePrice());
            storeProductResult.setSalePrice(storeProductDb.getSalePrice());
            storeProductResult.setShelfStatus(storeProductDb.getShelfStatus());
            storeProductResult.setMainImage(aliyunOssTool.getImgUrl(productResult.getMainImage()));
            storeProductResultList.add(storeProductResult);
        }
        return storeProductResultList;
    }
    
    /**
     * 获取店铺产品BEAN列表
     * @param storeProductList List<{@link StoreProduct}>
     * @return List<{@link StoreProductResult}>
     */
    public List<ActivityProductResult> getActivityProductResultList(List<StoreProduct> storeProductList) {
        List<ActivityProductResult> storeProductResultList = new ArrayList<ActivityProductResult>();

        for (StoreProduct storeProductDb : storeProductList) {
            ProductResult productResult = productService.getProductBeanById(storeProductDb.getProductId());
            ActivityProductResult storeProductResult = ActivityProductResult.covertToBean(productResult);
            storeProductResult.setId(storeProductDb.getProductId());
            storeProductResult.setCostPrice(productResult.getSalePrice());
            storeProductResult.setSalePrice(storeProductDb.getSalePrice());
            storeProductResult.setMainImage(aliyunOssTool.getImgUrl(productResult.getMainImage()));
            storeProductResultList.add(storeProductResult);
        }
        return storeProductResultList;
    }

    /**
     * APP搜索统计
     * @param storeProduct
     * @return
     */
    public long count(StoreProductBean storeProduct) {
        StoreProductExample example = new StoreProductExample();
        setOrCriterial(storeProduct, example);
        return storeProductMapper.countByExample(example);
    }

    /**
     * 设置查询条件
     *
     * @param criteria
     * @param storeProduct
     */
    private void setOrCriterial(StoreProductBean storeProduct, StoreProductExample example) {
        StoreProductExample.Criteria criteria1 = example.createCriteria();
        if (storeProduct.getShelfStatus() != null) {
            criteria1.andShelfStatusEqualTo(storeProduct.getShelfStatus());
        }
        criteria1.andStoreIdEqualTo(storeProduct.getStoreId());
        criteria1.andDelFlagEqualTo(StoreProduct.DEL_FLAG_NORMAL);
        if (StringUtils.isNotBlank(storeProduct.getProductCode())) {
            criteria1.andProductCodeLike("%" + storeProduct.getProductCode() + "%");
        }
        StoreProductExample.Criteria criteria2 = example.createCriteria();
        criteria2.andStoreIdEqualTo(storeProduct.getStoreId());
        if (storeProduct.getShelfStatus() != null) {
            criteria2.andShelfStatusEqualTo(storeProduct.getShelfStatus());
        }
        criteria2.andDelFlagEqualTo(StoreProduct.DEL_FLAG_NORMAL);
        if (StringUtils.isNotBlank(storeProduct.getProductName())) {
            criteria2.andProductNameLike("%" + storeProduct.getProductName() + "%");
        }
        example.or(criteria2);
    }

    /**
     * APP搜索列表
     * @param offset
     * @param limit
     * @param storeProduct
     * @return
     */
    public List<StoreProduct> getList(Integer offset, Integer limit, StoreProductBean storeProduct) {
        StoreProductExample example = new StoreProductExample();
        example.setOrderByClause("id DESC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        setOrCriterial(storeProduct, example);
        return storeProductMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 查询店铺里的产品Id
     * @param storeId
     * @return
     */
    public List<StoreProduct> getStoreProductListByStoreId(Integer storeId){

        List<StoreProduct> storeProductList = new ArrayList<StoreProduct>();
        if (storeId == null) {
            return storeProductList;
        }else{
            StoreProductExample example=new StoreProductExample();
            StoreProductExample.Criteria criteria = example.createCriteria();
            criteria.andStoreIdEqualTo(storeId);
            criteria.andShelfStatusEqualTo(1);
            criteria.andDelFlagEqualTo(StoreProduct.DEL_FLAG_NORMAL);
            storeProductList = storeProductMapper.selectByExample(example);
        }
        return storeProductList;
    }

    /**
     * 根据店铺ID查询店铺产品
     * @param storeProduct
     * @return
     */
    public List<StoreProduct> storeToSelect(StoreProduct storeProduct){
        StoreProductExample example = new StoreProductExample();
        StoreProductExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria,storeProduct);
        List<StoreProduct> list= storeProductMapper.selectByExample(example);
        return list;
    }

    /**
     * 当前用户的店铺产品列表
     * @return
     */
    public List<ActivityProductResult> getActivityProductResultList() {
        Integer storeId = userService.queryStoreId();
        List<StoreProduct> storeProductList = getStoreProductListByStoreId(storeId);
        List<ActivityProductResult> resultList =  getActivityProductResultList(storeProductList);
        return resultList;
    }
}
